//
//  LDYComposeToolBar.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/23.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit
enum LDYComposeToolBarType : Int {
    //图片
    case picture = 1001
    // @
    case Mention = 1002
    //#
    case Trend = 1003
    //表情
    case Emoticon = 1004
    //+
    case Add = 1005
}


class LDYComposeToolBar: UIView {

    //判断是否是表情键盘
    var isEmoticon: Bool = false{
    
        didSet{
    
            if isEmoticon{
                print("键盘图片")
                pictureButton4?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                pictureButton4?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{
                print("表情图片")
                pictureButton4?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                pictureButton4?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        
        
        }
    
    }
    
    var pictureButton4: UIButton?
    //传值闭包
    var composeToolBarColsure:((type: LDYComposeToolBarType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
    
    self.backgroundColor = randomColor()
    
        //添加控件
        let pictureButton1 = addChildButton("compose_toolbar_picture", type: .picture)
        let pictureButton2 = addChildButton("compose_mentionbutton_background", type: .Mention)
        let pictureButton3 = addChildButton("compose_trendbutton_background", type: .Trend)
        pictureButton4 = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let pictureButton5 = addChildButton("compose_add_background", type: .Add)
        
         pictureButton1.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(pictureButton2)
          
        }
        
        pictureButton2.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(pictureButton3)
            make.leading.equalTo(pictureButton1.snp_trailing)
            
        }
        pictureButton3.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(pictureButton4!)
            make.leading.equalTo(pictureButton2.snp_trailing)
        }
        pictureButton4!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(pictureButton5)
            make.leading.equalTo(pictureButton3.snp_trailing)
        }
        pictureButton5.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(pictureButton4!.snp_trailing)
        }
        
        
        
    }
    
    
    
    //MARK: - 监听事件
    @objc private func buttonClick(btn: UIButton){
    
     composeToolBarColsure?(type: LDYComposeToolBarType(rawValue: btn.tag)!)
    
    }

    
    
    //MARK: -  添加按钮的公共方法
    func addChildButton(imgName:String , type: LDYComposeToolBarType) -> UIButton{
    let but = UIButton()
    
      but.tag = type.rawValue
      //添加点击事件
        but.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
    but.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
    but.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
    but.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
    but.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(but)
    return but
    }

    
}
